package service.array;

import dto.Deptdto;
import entity.Dept;
import service.IDeptService;
import service.IUserService;
import vo.Deptvo;
import vo.Result;

public class DeptService extends BaseService<Dept, Deptdto, Deptvo> implements IDeptService {

    private IUserService userService;

    private static IDeptService singleton;

    private DeptService(){
        data  = new Dept[DATA_SIZE+1];
        temp  = new Deptvo[DATA_SIZE];
    }
    public static IDeptService getInstance(){
        if(singleton == null){
            singleton = new DeptService();
        }
        return singleton;
    }

    @Override
    Deptvo convertToVo(Dept dept) {
        return new Deptvo(dept.getId(), dept.name, dept.phone);
    }

    @Override
    Dept createEntity(Deptdto dto, int id) {
        return new Dept(id,dto.name, dto.phone);
    }

    @Override
    void updateEntity(Deptdto dto, Dept dept) {
        dept.name  = dto.name;
        dept.phone = dto.phone;
    }

    @Override
    public Result delete(int id){
        if(!existById(id)){
            return Result.failure("删除失败,不存在该数据");
        }
        if(userService == null){
            userService = UserService.getInstance();
        }
        if(userService.deptInUse(id)){
            return Result.failure("删除失败,还有员工属于该部门,不能删除");
        }
        data[id]=null;
        return Result.success("删除成功",null);
    }
}